package com.controller;

import com.bean.Emp;
import com.bean.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.EmpService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class ListController {
    @Autowired
    EmpService empService;

    @RequestMapping(value = "/list")
    public String toList(@RequestParam(value = "pageNumber",defaultValue = "1") int pageNumber,Model model){
        Map map=new HashMap();
        //(页号，页大小)
        PageHelper.startPage(pageNumber,6);
        //startPage后面紧跟的就是一个分页查询
        List<Emp> empList= empService.getAllEmp(map);
        //用PageInfo对结果进行包装(传入连续显示的页数)
        PageInfo pageInfo=new PageInfo(empList,7);
        model.addAttribute("pageInfo",pageInfo);
        System.out.println(pageInfo);
        return "list";
    }
    @RequestMapping(value = "/listWithAjax")
    @ResponseBody
    public Msg toListWithAjax(@RequestParam(value = "pageNumber",defaultValue = "1") int pageNumber,Model model){
        Map map=new HashMap();
        //(页号，页大小)
        PageHelper.startPage(pageNumber,6);
        //startPage后面紧跟的就是一个分页查询
        List<Emp> empList= empService.getAllEmp(map);
        //用PageInfo对结果进行包装(传入连续显示的页数)
        PageInfo pageInfo=new PageInfo(empList,7);
        model.addAttribute("pageInfo",pageInfo);
        return Msg.success().add("pageInfo",pageInfo);
    }
}
